package com.example.sunlight.interneteverything.View;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.drawable.BitmapDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;

import com.example.sunlight.interneteverything.R;

/**
 * Created by SunLight on 2017/8/16.
 */

public class ViewLeaf extends View {
    Bitmap one, two, three;
    Resources resources;
    Rotate rotate;
    TypedArray typedArray;
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0x123:
                    ViewLeaf.this.invalidate();
                    break;
            }
        }
    };
    Paint paint;

    public ViewLeaf(Context context) {
        super(context);
        init();

    }

    public void init() {
        resources = getResources();
        rotate = new Rotate();
        rotate.start();
        paint = new Paint();

    }

    String bg;
    int title_top, title_left;
    int bottom_top, bottom_left;
    String type, number;
    int type_size, number_size;
    int type_top, type_left;
    int number_top, number_left;

    public void setNumber(String number) {
        this.number = number;
        handler.sendEmptyMessage(0x123);
    }

    public void init(AttributeSet attrs) {
        typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.ViewLeaf);
        bg = typedArray.getString(R.styleable.ViewLeaf_tree_bg);
        title_top = (int) typedArray.getDimension(R.styleable.ViewLeaf_title_top, 0);
        title_left = (int) typedArray.getDimension(R.styleable.ViewLeaf_title_left, 0);
        bottom_top = (int) typedArray.getDimension(R.styleable.ViewLeaf_bottom_top, 0);
        bottom_left = (int) typedArray.getDimension(R.styleable.ViewLeaf_bottom_left, 0);
        type = typedArray.getString(R.styleable.ViewLeaf_type);
        number = typedArray.getString(R.styleable.ViewLeaf_number);
        type_size = (int) typedArray.getDimension(R.styleable.ViewLeaf_type_size, 1);
        number_size = (int) typedArray.getDimension(R.styleable.ViewLeaf_number_size, 1);

        type_top = (int) typedArray.getDimension(R.styleable.ViewLeaf_type_top, 1);
        type_left = (int) typedArray.getDimension(R.styleable.ViewLeaf_type_left, 1);

        number_top = (int) typedArray.getDimension(R.styleable.ViewLeaf_number_top, 1);
        number_left = (int) typedArray.getDimension(R.styleable.ViewLeaf_number_left, 1);
        selectorbim(bg);
        with = one.getWidth();
        height = one.getHeight();
    }

    public ViewLeaf(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
        init(attrs);
    }

    public ViewLeaf(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
        init(attrs);
    }

    int with, height;

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(with, height);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        drawRotateBitmap(canvas, null, one, i, 0, 0);
        drawRotateBitmap(canvas, null, two, 0, title_left, title_top);
        drawSizeText(canvas, paint, type, type_size, type_left, type_top);
        drawSizeText(canvas, paint, number, number_size, number_left, number_top);
        drawRotateBitmap(canvas, null, three, 0, bottom_left, bottom_top);
    }

    int i = 0;


    private void drawRotateBitmap(Canvas canvas, Paint paint, Bitmap bitmap, float rotation, float posX, float posY) {
        Matrix matrix = new Matrix();
        int offsetX = bitmap.getWidth() / 2;
        int offsetY = bitmap.getHeight() / 2;
        matrix.postTranslate(-offsetX, -offsetY);
        matrix.postRotate(rotation);
        matrix.postScale(0.85f, 0.85f);
        matrix.postTranslate(posX + offsetX, posY + offsetY);
        canvas.drawBitmap(bitmap, matrix, paint);
    }

    private void drawSizeText(Canvas canvas, Paint paint, String text, int size, float posX, float posY) {
        paint.setAntiAlias(true);
        paint.setColor(Color.WHITE);
        paint.setTextSize(size);
        canvas.drawText(text, posX, posY, paint);
    }

    class Rotate extends Thread {
        @Override
        public void run() {
            try {
                while (true) {
                    Thread.sleep(45);
                    if (i <= 360) {
                        if (i % 60 == 0) {
                            Thread.sleep(500);
                        }
                        i++;
                    } else {
                        i = 0;
                    }
                    handler.sendEmptyMessage(0x123);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

    private void selectorbim(String name) {
        switch (name) {
            case "one":
                one = ((BitmapDrawable) resources.getDrawable(R.drawable.one_leaf)).getBitmap();
                two = ((BitmapDrawable) resources.getDrawable(R.drawable.leaf_red)).getBitmap();
                three = ((BitmapDrawable) resources.getDrawable(R.drawable.tree_guan)).getBitmap();
                break;
            case "two":
                one = ((BitmapDrawable) resources.getDrawable(R.drawable.two_leaf)).getBitmap();
                two = ((BitmapDrawable) resources.getDrawable(R.drawable.leaf_wihte)).getBitmap();
                three = ((BitmapDrawable) resources.getDrawable(R.drawable.tree_fa)).getBitmap();
                break;
            case "three":
                one = ((BitmapDrawable) resources.getDrawable(R.drawable.three_leaf)).getBitmap();
                two = ((BitmapDrawable) resources.getDrawable(R.drawable.leaf_blue)).getBitmap();
                three = ((BitmapDrawable) resources.getDrawable(R.drawable.tree_wen)).getBitmap();
                break;
            case "four":
                one = ((BitmapDrawable) resources.getDrawable(R.drawable.four_leaf)).getBitmap();
                two = ((BitmapDrawable) resources.getDrawable(R.drawable.leaf_pink)).getBitmap();
                three = ((BitmapDrawable) resources.getDrawable(R.drawable.tree_shi)).getBitmap();
                break;
        }
    }
}
